import java.util.Random;
import javax.swing.DefaultListModel;

/**
 * Person class simulates a person.
 * The person has a name, an age and a shoppinbasket with goods in it.
 * @author Martin
 *
 */
public class Person {
	//	Local variables		
	private String _name, _age;
	private DefaultListModel _shoppingBasket;
	private Random _r;
	private int _numOfItems, _item, _totalCost;
	private final int _MaxItems = 20;
	
	/**
	 * Constructor
	 */
	public Person(String aName, String aAge)
	{
		_name = aName;
		_age = aAge;
		_r = new Random();
		_shoppingBasket = new DefaultListModel();
		ShoppingList();
	}
	
	/**
	 * Fills the shopping basket with items.
	 * Pre: true
	 * Post: fills the _shoppingBasket with goods.
	 */
	private void ShoppingList()
	{	
		_numOfItems = _r.nextInt(_MaxItems - 1) + 1;
		_totalCost = 0;
		GoodsBread Bread = new GoodsBread();
		GoodsMilk Milk = new GoodsMilk();
		GoodsTomato Tomato = new GoodsTomato();
		int i = 0;
		while (i<_numOfItems)
		{
			_item = _r.nextInt(3);
			switch (_item)
			{
			case 0:
				_shoppingBasket.addElement(Bread);
				_totalCost = _totalCost + Bread.getPrice();
				break;
			case 1:
				_shoppingBasket.addElement(Milk);
				_totalCost = _totalCost + Milk.getPrice();
				break;
			case 2:
				_shoppingBasket.addElement(Tomato);
				_totalCost = _totalCost + Tomato.getPrice();
				break;
			}
			i++;
		}
	}
	
	/**
	 * Returns a formatted string with this persons name and age.
	 * Pre: true
	 * Post: Returns a formatted string.
	 */
	public String toString() {
		return "" + _name + " " + _age;
	}

	/**
	 * Returns the name of the person.
	 * Pre: true
	 * Post: Returns the name of the person.
	 */
	public String get_name() {
		return _name;
	}

	/**
	 * Returns the age of the person.
	 * Pre: true
	 * Post: Returns the age of the person.
	 */
	public String get_age() {
		return _age;
	}
	
	/**
	 * Returns the shopping basket of the person.
	 * Pre: a person must have been selected.
	 * Post: Returns the shopping basket of the person.
	 */
	public DefaultListModel get_shoppingBasket() {
		return _shoppingBasket;
	}
	
	/**
	 * Returns the total cost of the goods in the shopping basket.
	 * Pre: a person must have been selected.
	 * Post: Returns the total cost of the goods in the shopping basket.
	 */
	public int get_totalCost() {
		return _totalCost;
	}

	/**
	 * Returns the number of items in the shoppingbasket.
	 * Pre: a person must have been selected.
	 * Post: Returns the number of items in the shoppingbasket.
	 */
	public int get_numOfItems() {
		return _numOfItems;
	}

	/**
	 * Sets the name of the person.
	 * Pre: a person must have been created.
	 * Post: Sets the name of the person.
	 */
	public void set_name(String _name) {
		this._name = _name;
	}

	/**
	 * Sets the age of the person.
	 * Pre: a person must have been created.
	 * Post: Sets the age of the person.
	 */
	public void set_age(String _age) {
		this._age = _age;
	}
}
